/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "uart_if.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_demo_def.h"
#include "hdf_log.h"
#include "iot_uart.h"

callback g_VoiceCtl;

DevHandle UartSetup(void)
{
    int32_t ret;
    uint32_t port;  

    struct UartAttribute attribute;
    attribute.dataBits = UART_ATTR_DATABIT_8;   /* UART传输数据位宽，一次传输7个bit */
    attribute.parity = UART_ATTR_PARITY_NONE;   /* UART传输数据无校检 */
    attribute.stopBits = UART_ATTR_STOPBIT_1;   /* UART传输数据停止位为1位 */
    attribute.rts = UART_ATTR_RTS_DIS;          /* UART禁用RTS */
    attribute.cts = UART_ATTR_CTS_DIS;          /* UART禁用CTS */
    attribute.fifoRxEn = UART_ATTR_RX_FIFO_DIS;  /* UART使能RX FIFO */
    attribute.fifoTxEn = UART_ATTR_TX_FIFO_DIS;  /* UART使能TX FIFO */
    /* UART设备端口号，要填写实际平台上的端口号 */
    port = 1; 
    /* 获取UART设备句柄 */
    HDF_LOGE("open uart 1");
    DevHandle handle = UartOpen(port);
    if (handle == NULL) {
        HDF_LOGE("UartOpen: failed!\r\n");
        return -2;
    }
    /* 设置UART波特率为9600 */
    ret = UartSetBaud(handle, 115200);
    if (ret != 0) {
        HDF_LOGE("UartSetBaud: failed, ret %d\r\n", ret);
        goto _ERR;
    }
    /* 设置UART设备属性 */
    ret = UartSetAttribute(handle, &attribute);
    if (ret != 0) {
        HDF_LOGE("UartSetAttribute: failed, ret %d\r\n", ret);
        goto _ERR;
    }
    /* 设置UART传输模式为非阻塞模式 */
    ret = UartSetTransMode(handle, UART_MODE_RD_BLOCK);
    if (ret != 0) {
        HDF_LOGE("UartSetTransMode: failed, ret %d\r\n", ret);
        goto _ERR;
    }

    return handle;
 
_ERR:
    /* 销毁UART设备句柄 */
    UartClose(handle); 
    return -1;
}

void UartTestSampleTask(void)
{
    uint8_t rbuff;
    uint8_t recvFifo[32];   
    uint8_t recvCount = 0;
    uint8_t recvAaCount = 0;
    uint8_t * pbuff = &rbuff;
    static uint8_t lastChar;
    int ret;
    
    DevHandle handle = UartSetup();
    if( handle < 0 ){
        HDF_LOGE("open_port error\r\n");
        return 1;
    }

    memset_s(recvFifo, 32, 0, 32);

    while (1) {

       *pbuff = 0;
        ret = UartRead(handle, pbuff, 1);
        if (ret < 0) {
            HDF_LOGE("UartRead: failed, ret %d\r\n", ret);
            goto _ERR;
        }

        if(*pbuff != 0) {
            if (lastChar == 0xaa && *pbuff == 0xaa) {
                recvCount = 0;
            }
            lastChar = *pbuff;
            recvFifo[recvCount] = *pbuff;
            recvCount++;

            HDF_LOGE("recvCount=%d.lastChar= 0x%x,*pbuff=0x%x\r\n",recvCount,*pbuff,lastChar);
        }

        /*aa 01 30 55 aa*/
        if (recvCount == 5) {
            if (recvFifo[0] == 0xaa && recvFifo[4] == 0xaa) {
                if (recvFifo[2] == 0x30) {
                    HDF_LOGE("voice off\r\n");
                    g_VoiceCtl(false);
                } else if(recvFifo[2] == 0x31) {
                    HDF_LOGE("voice on\r\n");
                    g_VoiceCtl(true);
                }
            }
            memset_s(recvFifo, 32, 0, 32);
        }
    }

_ERR:
    /* 销毁UART设备句柄 */
    UartClose(handle); 
}

void IotUart_StartUartThread(callback p_callback)
{
    osThreadAttr_t attr = {0};

    HDF_LOGE("UartTestSample Entry\r\n");
    attr.stack_size = 0x1000; 
    attr.priority = osPriorityNormal;
    attr.name = "UartTestSample";
    if (p_callback != NULL ) {
        g_VoiceCtl = p_callback;
    }
    if (osThreadNew((osThreadFunc_t)UartTestSampleTask, NULL, &attr) == NULL) {
        HDF_LOGE("Failed to create UartTestSampleTask\r\n");
    }
}







